using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NCT.Framework;
using System.Data;
using System.Data.Common;
using NCT.Framework.Database;

namespace VISS.API.Users {
	class UserImageDA {

		private const string SP_Delete = "dbo.UserImage_Delete";
		private const string SP_GetByUserKey = "dbo.UserImage_GetByUserKey";
        private const string SP_GetByUserKeyAndID = "dbo.UserImage_GetByUserKeyAndID";
        private const string SP_GetByUserKeyWithPaging = "dbo.UserImage_GetByUserKeyWithPaging";
        private const string SP_GetAll = "dbo.UserImage_GetAll";
		private const string SP_Insert = "dbo.UserImage_Insert";

		public static UserImageEnt CreateFromReader(IDataReader reader) {
			UserImageEnt item = new UserImageEnt();
			item.ID = ConvertUtils.ToLong(reader["ID"]);
			item.UserKey = ConvertUtils.ToString(reader["UserKey"]);
			item.FileName = ConvertUtils.ToString(reader["FileName"]);
            item.ThumbFileName = ConvertUtils.ToString(reader["ThumbFileName"]);  
			item.Description = ConvertUtils.ToString(reader["Description"]);
			item.ImageStoreID = ConvertUtils.ToInt(reader["ImageStoreID"]);
			item.DateCreated = ConvertUtils.ToDateTime(reader["DateCreated"]);
			return item;
		}

        public static int Delete(string userKey, long iD, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
			int result = -1;
			Database db = Database.CreateDatabase(connectionName);
			using (DbCommand cmd = db.CreateStoredCommand(SP_Delete)) {
                db.AddParameter(cmd, "UserKey", userKey);
				db.AddParameter(cmd, "ID", iD);
				if (dbCon != null && dbTrans != null) {
					result = db.ExecuteNonQuery(cmd, dbCon, dbTrans);
				}
				else {
					result = db.ExecuteNonQuery(cmd);
				}
			}
			return result;
		}

		public static List<UserImageEnt> GetByUserKey(string userKey, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
			List<UserImageEnt> result = new List<UserImageEnt>();
			Database db = Database.CreateDatabase(connectionName);
			using (DbCommand cmd = db.CreateStoredCommand(SP_GetByUserKey)) {
				db.AddParameter(cmd, "UserKey", userKey);
				using (IDataReader reader = db.ExecuteReader(cmd)) {
					while (reader.Read()) {
						result.Add(CreateFromReader(reader));
					}
				}
			}
			return result;
		}

        public static UserImageEnt GetByUserKeyAndID(string userKey, long iD, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
            UserImageEnt result = null;
            Database db = Database.CreateDatabase(connectionName);
            using (DbCommand cmd = db.CreateStoredCommand(SP_GetByUserKeyAndID)) {
                db.AddParameter(cmd, "UserKey", userKey);
                db.AddParameter(cmd, "ID", iD);
                using (IDataReader reader = db.ExecuteReader(cmd)) {
                    if (reader.Read()) {
                        result = CreateFromReader(reader);
                    }
                }
            }
            return result;
        }

        public static List<UserImageEnt> GetByUserKeyWithPaging(string userKey, int startRow, int endRow, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
            List<UserImageEnt> result = new List<UserImageEnt>();
            Database db = Database.CreateDatabase(connectionName);
            using (DbCommand cmd = db.CreateStoredCommand(SP_GetByUserKeyWithPaging)) {
                db.AddParameter(cmd, "UserKey", userKey);
                db.AddParameter(cmd, "StartRow", startRow);
                db.AddParameter(cmd, "EndRow", endRow);
                using (IDataReader reader = db.ExecuteReader(cmd)) {
                    while (reader.Read()) {
                        result.Add(CreateFromReader(reader));
                    }
                }
            }
            return result;
        }

        public static List<UserImageEnt> GetAll(string userKey, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
            List<UserImageEnt> result = new List<UserImageEnt>();
            Database db = Database.CreateDatabase(connectionName);
            using (DbCommand cmd = db.CreateStoredCommand(SP_GetAll)) {
                using (IDataReader reader = db.ExecuteReader(cmd)) {
                    while (reader.Read()) {
                        result.Add(CreateFromReader(reader));
                    }
                }
            }
            return result;
        }

        public static int Insert(UserImageEnt item, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
			int result = -1;
			Database db = Database.CreateDatabase(connectionName);
			using (DbCommand cmd = db.CreateStoredCommand(SP_Insert)) {
				db.AddParameterOuter(cmd, "ID", DbType.Int64);
				db.AddParameter(cmd, "UserKey", item.UserKey);
				db.AddParameter(cmd, "FileName", item.FileName);
                db.AddParameter(cmd, "ThumbFileName", item.ThumbFileName);
				db.AddParameter(cmd, "Description", item.Description);
				db.AddParameter(cmd, "ImageStoreID", item.ImageStoreID);
				db.AddParameter(cmd, "DateCreated", item.DateCreated);

				if (dbCon != null && dbTrans != null) {
					result = db.ExecuteNonQuery(cmd, dbCon, dbTrans);
                    item.ID = ConvertUtils.ToLong(cmd.Parameters["ID"].Value);
				}
				else {
					result = db.ExecuteNonQuery(cmd);
                    item.ID = ConvertUtils.ToLong(cmd.Parameters["ID"].Value);
				}
			}
			return result;
		}

	}
}